home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_Tix.idb / usr / freeware / lib / tix4.1 / demos / samples / CObjView.tcl.z / CObjView.tcl
Encoding:
Text File  |  1999-01-26  |  2.6 KB  |  86 lines

  1. # Tix Demostration Program
  2. #
  3. # This sample program is structured in such a way so that it can be
  4. # executed from the Tix demo program "widget": it must have a
  5. # procedure called "RunSample". It should also have the "if" statment
  6. # at the end of this file so that it can be run as a standalone
  7. # program using tixwish.
  8.  
  9. # This program demonstrates the use of the CObjView (Canvas Object
  10. # View) class.
  11. #
  12.  
  13.  
  14. proc RunSample {w} {
  15.     label $w.lab  -justify left -text \
  16. "Click on the buttons to add or delete canvas
  17. objects randomally. Notice the scrollbars automatically
  18. adjust to include all objects in the scroll-region."
  19.  
  20.     pack $w.lab -anchor c -padx 10 -pady 6
  21.     tixCObjView $w.c
  22.     pack $w.c -expand yes -fill both -padx 4 -pady 2
  23.     button $w.add -command "CVDemo_Add $w.c"    -text Add    -width 6
  24.     button $w.del -command "CVDemo_Delete $w.c" -text Delete -width 6
  25.     pack $w.add $w.del -side left -padx 20 -pady 10 -anchor c -expand yes
  26. }
  27.  
  28. set cvdemo_counter 0
  29. proc CVDemo_Add {cov} {
  30.     global cvdemo_counter
  31.  
  32.     # Generate four pseudo random numbers (x,y,w,h) to define the coordinates
  33.     # of a rectangle object in the canvas.
  34.     #
  35.     set colors {red green blue white black gray yellow}
  36.  
  37.     set px [expr [lindex [time update] 0] + $cvdemo_counter]
  38.     set py [expr [lindex [time update] 0] + $cvdemo_counter]
  39.     set pw [expr [lindex [time update] 0] + $cvdemo_counter]
  40.     set ph [expr [lindex [time update] 0] + $cvdemo_counter]
  41.     set pc [expr [lindex [time update] 0] + $cvdemo_counter]
  42.  
  43.     set x [expr (20 - ($px % 37))   * 10]
  44.     set y [expr (10 - ($py % 23))  * 10]
  45.     set w [expr ($pw % 17)  * 10]
  46.     set h [expr ($ph % 17)  * 10]
  47.  
  48.     # Create the canvas object
  49.     #
  50.     $cov subwidget canvas create rectangle $x $y [expr $x+$w] [expr $y+$h] \
  51.     -fill [lindex $colors [expr $pc % [llength $colors]]]
  52.  
  53.     # Call the adjustscrollregion command to set the scroll bars so that all
  54.     # objects are included in the scroll-region
  55.     #
  56.     $cov adjustscrollregion
  57.  
  58.     # This number acts as the seed for the next round of randomization.
  59.     #
  60.     set cvdemo_counter [expr ($px % 37)]
  61. }
  62.  
  63. proc CVDemo_Delete {cov} {
  64.     set px [lindex [time update] 0]
  65.     set w [$cov subwidget canvas]
  66.     set items [$w find withtag all]
  67.  
  68.     if [string compare $items ""] {
  69.     # There are items in the canvas, randomally delete one of them
  70.     # and re-adjust the scroll-region
  71.     #
  72.     set toDelete [expr $px % [llength $items]]
  73.     $w delete [lindex $items $toDelete]
  74.  
  75.     $cov adjustscrollregion
  76.     }
  77. }
  78.  
  79. if {![info exists tix_demo_running]} {
  80.     wm withdraw .
  81.     set w .demo
  82.     toplevel $w
  83.     RunSample $w
  84.     bind $w <Destroy> exit
  85. }
  86.